/**
 * Created by mac on 2017/8/1.
 */
var is_have_login = "";
//ROOT  = window.location.host;
$(function () {
    $('#input_money').on('input propertychange',
        function() {
            gerProfit();
        });


    //关闭弹窗
    $(".back").on("click", function (e) {

        //clearInterval(timer_countDown);
        var p_btm = $("#buildBox,#buildConfirm,.masklayer");
        p_btm.hide();
        var type = $("#time_diff a.changed").attr("type");
        var diff = type * 60;
        e.preventDefault();

    });

    $(".up").on("click", function () {
        check_login(function () {
            order_dirt =1;
            $("#_buy_type").val(1);
            var money = 0;
            $(".active ul").find('li').siblings().removeClass('slct');
            $(".active ul").find('li').eq(4).addClass('slct');
            var money = $(".slct p i").text();
            if (money == '') {
                money = $("#input_money").val();
            }
            //times = $(".swiper-slide-active h4 span").html();
            times = $(".swiper-slide-active").find(".digspan").html();
            times = times.replace(" ", "");
            times = parseInt(times);
            var bili = set_profit(times);
            $("#input_money").val(money);
            $('.active h1 b').text(money);
            var yuqi = 0;
            //alert(money );
            //alert( bili );
            var yuqi = money * bili;
            yuqi = parseFloat(yuqi) + parseFloat(money);
            $('.active h1 i').html('预期收益：<span>' + yuqi + '</span>元');

            $('#buildBox').show();
            //alert( $("#BTbtccny").html() );
            //$("#flow_span_value").html( $("#"+$("#cur_show_good").val()).html() );

            pop_Open();
            set_order_message();
            $("#flow_span_value").html( $("#"+$("#cur_show_good").val()).html() );

        });
        return false;
    });

    $(".down").on("click", function () {
        check_login(function(){
            order_dirt = 0;
            $("#_buy_type").val(-1);
            var money = 0;
            $(".active ul").find('li').siblings().removeClass('slct');
            $(".active ul").find('li').eq(4).addClass('slct');
            var money = $(".slct p i").text();
            if (money == '') {
                money = $("#input_money").val();
            }
            times = $(".swiper-slide-active").find(".digspan").html();
            times = times.replace(" ", "");
            times = parseInt(times);
            var bili = set_profit(times);
            $("#input_money").val(money);
            $('.active h1 b').text(money);
            var yuqi = 0;
            var yuqi = money * bili;
            yuqi = parseFloat(yuqi) + parseFloat(money);
            $('.active h1 i').html('预期收益：<span>' + yuqi + '</span>元');
            $('#buildBox').show();
            pop_Open();
            set_order_message();
            $("#flow_span_value").html( $("#"+$("#cur_show_good").val()).html() );

        });

    });

    function gerProfit(){
        $(".slct").removeClass("slct");
        var money = $("#input_money").val();
        if(money > 5000){
            alert('最高只能输入5000金额');
            $("#input_money").val(5000);
            return false;
        }

        times = $(".swiper-slide-active").find(".digspan").html();
        times = times.replace(" ", "");
        times = parseInt(times);
        var bili = set_profit(times);


        var yuqi = 0;
        var yuqi = money * bili;
        yuqi = parseFloat(yuqi) + parseFloat(money);
        $('.active h1 i').html('预期收益：<span>' + yuqi + '</span>元');

    }
    /*判断num是否是数字*/
    function IsNum(num){
        var reNum=/^\d*$/;
        return(reNum.test(num));
    }


    $('#pop-reg-btn-login').click(function(){
        window.location = ROOT + '/Wap/login/login_show';
    });


    //注册
    $('#pop-reg-btn-new').click(function(){
        var phone = $("#pop-reg-phone").val().trim();
        var passwd = $("#pop-reg-pwd").val().trim();
        var SMSCode = $('#pop-reg-code').val().trim();
        var shareid = $('#pop-reg-shareid').val().trim();


        if(check_reg_data())
        {
            if(SMSCode=="")
            {
                showLoading('请输入验证码', 3000);
                return ;
            }
            $.ajax({
                type : 'POST',
                url : ROOT + '/Wap/Login/regist_user',
                data : 'shareid='+shareid+'&passwd=' + passwd + '&tel=' + phone + '&token=' + "" + '&reg_xieyi=1&proxy_id=' + ""+"&SMSCode="+SMSCode+"&inviterMobile="+"" +"&act="+"",
                dataType : 'JSON',
                async : false,
                success : function(data_t) {
                    if (data_t['status'] == 1) {
                        window.location = ROOT + '/Wap/Trading/index';
                    } else {
                        showLoading(data_t['info'], 2000);
                        return false;
                    }
                },
                error : function(data) {
                    return false;
                },
            });
        }
    });




    $('.pop-reg').click(function(){
        return false;
    });


    $('.active ul li:not(.not)').click(function () {
        $(this).addClass('slct').siblings().removeClass('slct');
        //var text=$(this).find('i').text();
        //$('.active h1 b').text(text);
        $('.active h2').show();
        $('.active ul li.not input').fadeOut();
    })
    $('.active ul li:not(.other)').click(function () {
        //times = $(".swiper-slide-active h4 span").html();
        times = $(".swiper-slide-active").find(".digspan").html();
        times = times.replace(" ", "");
        times = parseInt(times);
        var money = $("#input_money").val();
        var bili = set_profit(times);
        var text = $(this).find('i').text();
        $('.active h1 b').text(text);/*购买价格*/
        var yuqi = 0;
        var yuqi = text * bili;
        yuqi = parseFloat(yuqi) + parseFloat(text);
        $('.active h1 i').html('' +
            '预期收益：<span>' + yuqi + '</span>元');

        //$('.active h2').hide();
    });

    $('.active ul li.not').click(function () {
        $(this).find('input').show().select();
        gerProfit();
    });


    $("#input_money").on("click",function(){
        var money = $("#input_money").val();
        console.log("点击时的money值"+money);
        if( money.length > 0 )
        {
            if(IsNum( money ))
            {
                gerProfit();
            }
        }
    });

    $('.prompt .but_sub').click(function() {
        var money = $(".slct p i").text();
        if(money==''){
            money = $("#input_money").val();

        }
        if(money<10){
            showLoading('最低充值金额为10元',2000);
        }else if (money<100) {
            $('.box_show').fadeIn();
        } else {
            goto_pay(money);
        }
    });


    $('#gotopay').click(function() {
        var money = $("#input_money").val();
        goto_pay(money);
    });
    $('#close_win').click(function() {

        $('.box_show').fadeOut();
    });
    $('.prompt span a').click(function() {
        if ($(".explain").is(":hidden")) {
            $('.explain').slideDown();
            $('html,body').animate({
                    scrollTop: $('#top').offset().top
                },
                1000);
        } else {
            $('.explain').slideUp();
        }
    })




});

function goto_pay(money) {
    showLoading('提交中...');
    setTimeout(jump(ROOT + '/Wap/User/weixinpay?total_fee=' + money), 2000);
}
function jump(url){
    window.location.href =url;
}

function showLoading(msg,time){
    //$("#msg").html(msg);
    //$(".loading-wrapper").show();
    //if(time>0){
    //    setTimeout("hideLoading()",time);
    //}
}

function check_login(callback)
{
    if(is_have_login=="login")
    {
        callback();
    }
    else
    {
        //检查是否已登录
        jQuery.ajax({
            type:'POST',
            url: "/wap/trading/check_login",
            dataType: 'JSON',
            async: true,
            error: function (data) {
                //无法获取信息则正常下单

                return 0;
            },
            success: function (data_t) {
                if (data_t['result'] == 1) {
                    is_have_login="login";
                    callback();
                } else {
                    //弹出登录界面
                    //window.location.href="Wap/Login/login_show";
                    $('.pop-reg').show();
                    pop_Open();
                }
                return 0;
            }
        });
    }
}
function set_order_message() {
    $("#flow_span_dir").text("");
    $("#flow_span").text("");
    $("#flow_span_value").text("");
    $("#flow_span_mount").text("");
    $("#flow_span_time").text("");
    $("#flow_span_profit").text("");

    if (order_dirt == 0) {
        $("#flow_span_dir").html("<font color=green>买跌</font>");
        $("#flow_span_dir1").html("<font color=green>买跌</font>");
    } else {
        $("#flow_span_dir").html("<font color=red>买涨</font>");
        $("#flow_span_dir1").html("<font color=red>买涨</font>");
    }

    var amount = jQuery("#deal_amount").val();
    //amount = amount.replace("金额￥ ", "");
    if (amount == 0) amount = default_order;

    $("#flow_span_mount").text("￥" + amount);
    //$("#flow_span_time").text(jQuery("#finish_time").text().trim());
    //var aaaa= $("#selectmenu4 ").val();
    //var times = $(".swiper-slide-active").attr("index");
    //var times = $(".swiper-slide-active").attr("index");
    times = $(".swiper-slide-active").find(".digspan").html();
    times = times.replace(" ", "");
    times = parseInt(times);
    $("#flow_span_time").text(times + '秒');

    var expect_profit = amount * (1 + WINNERT);
    $("#flow_span_profit").text(expect_profit);
    $("#flow_span").text(get_cap_name());
}

function set_profit(valu_t) {
    if (valu_t == 60) {
        //return 0.85;
        return 0.8;
    } else if (valu_t == 120) {
        return 0.83;
    } else if (valu_t == 150) {
        return 0.86;
    }
}

//判断是否允许1分钟下单
//
//
function get_cap_name() {
    //alert( $(".goods").find(".current").find(".optionname").html() );
    var name = $(".goods").find(".current").find(".optionname").html();
    return name;
}

function check_data() {
    var tel = $("#username").val();
    var passwd = $("#password").val();
    if (tel == "") {
        showLoading('请输入手机号', 3000);
        return false;
    } else {
        if (!$("#username").val().match(/^(((1[0|3|4|5|7|8][0-9]{1}))+\d{8})$/)) {
            showLoading('手机号不正确', 3000);
            return false;
        } else {
            return true;
        }
    }
    if (passwd == "") {
        showLoading('请输入密码', 3000);
        return false;
    }
    if (passwd.length < 6 || passwd.length > 20) {
        showLoading('请输入大于6位小于20位的密码', 3000);
        return false;
    }
    return true;
}

function check_reg_data() {
    var tel = $("#pop-reg-phone").val().trim();
    var passwd = $("#pop-reg-pwd").val().trim();
    if (tel == "") {
        showLoading('请输入手机号', 3000);
        return false;
    } else {
        if (!$("#pop-reg-phone").val().match(/^(((1[0|3|4|5|7|8][0-9]{1}))+\d{8})$/)) {
            showLoading('手机号不正确', 3000);
            return false;
        }
    }
    if (passwd == "") {
        showLoading('请输入密码', 3000);
        return false;
    }
    if (passwd.length < 6 || passwd.length > 20) {
        showLoading('请输入大于6位小于20位的密码', 3000);
        return false;
    }
    return true;
}

function get_sms() {

    var verify_code = $("#pop-reg-code").val();
    var phone = $("#pop-reg-phone").val();
    var data = {"phone": phone, "verify_code": verify_code}
    $.post(ROOT + '/Wap/Login/img_send_sms_data', {
        data: JSON.stringify(data)
    }, function (result) {
        if (result.status == 0) {
            showLoading(result.message, 1000);
        } else {
            var time = 60;

            function timeCountDown() {
                if (time == 0) {
                    clearInterval(timer);
                    $("#send").attr("disabled", "");
                    $("#send").attr("value", "获取验证码");
                    return true;
                } else {
                    $("#send").attr("disabled", "disabled");
                    $("#send").attr("value", time + "s后重发");
                    time--;
                    return false;
                }
            }

            timeCountDown();
            var timer = setInterval(timeCountDown, 1000);
            //InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
            showLoading("验证码已发送！", 1000);
        }
    }, "json")
}

